package com.aijia.custom.product.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.aijia.custom.product.R;
import com.aijia.custom.product.adapter.OrderAdapter;
import com.aijia.custom.product.customview.LoadingWaitDialog;
import com.aijia.custom.product.customview.NoDoubleClickListener;
import com.aijia.custom.product.data.WSheetData;
import com.aijia.custom.product.listener.IWSheetListMyListener;
import com.aijia.custom.product.manager.WSheetListMyManager;
import com.aijia.custom.product.utils.Notification;
import com.powerfulrecyclerview.listener.OnRefreshListener;
import com.powerfulrecyclerview.ptr.HistoryThemeHeaderView;
import com.powerfulrecyclerview.ptr.PowerfulRecyclerView;
import com.powerfulrecyclerview.ptr.SimDividerItemDecoration;

import java.util.List;

/**
 * Created by yejianjun on 2019/5/11.
 */

public class PartInActivity extends Activity implements IWSheetListMyListener, OnRefreshListener, OrderAdapter.OnItemClickListener {

    private WSheetListMyManager manager;
    private int startPage = 0;
    private final int ROW = 10;
    private ImageButton backBtn;
    private TextView titleTV;

    private PowerfulRecyclerView recyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private HistoryThemeHeaderView header;
    // 是否上拉加载更多
    private int lastVisibleItem = 0;

    private OrderAdapter adapter;
    private Dialog pDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part_in);
        initView();
        initData();
        initManager();
        reqData();
    }

    private void initView() {
        initTitle();
        recyclerView = findViewById(R.id.order_listview);
    }

    private void initTitle() {
        titleTV = findViewById(R.id.title_tv);
        backBtn = findViewById(R.id.left_imageButton);
        backBtn.setVisibility(View.VISIBLE);
        titleTV.setText("我参与订单");
        backBtn.setOnClickListener(noDoubleClickListener);
    }

    private void initData() {
        adapter = new OrderAdapter(this);
        adapter.setOnItemClickListener(this);
        adapter.changeMoreStatus(OrderAdapter.PULLUP_LOAD_MORE);

        recyclerView.setAdapter(adapter);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        // 设置布局管理器
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.addItemDecoration(new SimDividerItemDecoration(this));
        // 设置adapter
        header = (HistoryThemeHeaderView) LayoutInflater.from(this).inflate(R.layout.history_header_theme, recyclerView, false);
        recyclerView.setHeaderView(header);
        recyclerView.setLoadMoreEnable(false);
        recyclerView.setRefreshing(true);
        recyclerView.setOnRefreshListener(this);
        initLoadMoreListener();
    }

    /**
     * 上拉加载更多
     */
    private void initLoadMoreListener() {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount() && adapter.getItemCount() >= ROW + 1) {
                    //设置正在加载更多
                    adapter.changeMoreStatus(OrderAdapter.LOADING_MORE);
                    startPage++;
                    reqData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }


    private void initManager() {
        manager = new WSheetListMyManager(this);
        manager.addWSheetListMyListener(this);

    }

    private void reqData() {
        manager.wSheetListMy(startPage * ROW, ROW);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onWSheetListMyFailure(int errorCode, String errorMsg) {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
        adapter.changeMoreStatus(OrderAdapter.NO_LOAD_MORE);
        recyclerView.stopRefresh();
    }

    @Override
    public void onWSheetListMySuccess(List<WSheetData> list) {
        adapter.changeMoreStatus(OrderAdapter.NO_LOAD_MORE);
        recyclerView.stopRefresh();
        if (startPage == 0) {
            adapter.clear();
            adapter.setData(list);
        } else {
            adapter.insertData(list);
        }
        adapter.notifyDataSetChanged();
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                if (null != pDialog && pDialog.isShowing()) {
                    pDialog.dismiss();
                }
            }
        });
    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.left_imageButton:
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    private void showDialog() {
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.show();
    }

    @Override
    public void onRefresh() {
        startPage = 0;
        reqData();
    }

    @Override
    public void itemClick(View view) {
        int position;
        switch (view.getId()) {
            case R.id.order_item_layout:
                position = (Integer) view.getTag(R.id.order_item_layout);

                Intent intent = new Intent();
                intent.setClass(this, OrderItemActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
